package com.incognito.spur.map.renderer;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.incognito.spur.map.Layer;
import com.incognito.spur.map.TiledMapException;
import com.incognito.spur.map.utility.Point;
import com.incognito.spur.map.utility.Viewport;

public class AndroidTiledMapRenderer extends TiledMapRenderer {

	private Bitmap tileset;

	/**
	 * Loads the tileset image.
	 * 
	 * @param filepath
	 * 		The path to the file
	 */
	public void loadTileset(String filepath) throws TiledMapException{
		try{
			tileset = BitmapFactory.decodeFile(filepath);
		}catch(Exception e){
			throw new TiledMapException(e);
		}
	}

	/**
	 * Draws the passed in layers on the provided graphics object.
	 * 
	 * @param obj
	 * 		The canvas object to draw to. This will be casted within
	 * 		the method.
	 * @param normalLayers
	 * 		The array of layers to draw
	 * @param viewport
	 * 		The portion of the map to actually draw. These are normal pixel
	 * 		coordinates. The tiles are still drawn in the top left corner.
	 * @param xOffset
	 * 		The offset from the left side to start drawing. This can be used
	 * 		to account for things like window decorations.
	 * @param yOffset
	 * 		The offset from the top side to start drawing. This can be used
	 * 		to account for things like window decorations.
	 */
	public void drawLayers(Object obj, ArrayList<Layer> layers, Viewport viewport, int xOffset, int yOffset) {

		if (obj != null && obj instanceof Canvas) {

			Canvas c = (Canvas) obj;
			
			for (Layer layer : layers){
				
				if (layer.isVisible() == true){
				
					int[][] grid = layer.getGrid();
					
					int startingY = viewport.getY() / tileHeight;
					int startingX = viewport.getX() / tileWidth;
					int endingY = (viewport.getY() + viewport.getHeight()) / tileHeight;
					int endingX = (viewport.getX() + viewport.getWidth()) / tileWidth;
					
					if (startingY < 0){
						startingY = 0;
					}
					
					if (startingX < 0){
						startingX = 0;
					}
					
					if (endingY > grid.length){
						endingY = grid.length;
					}
					
					if (endingX > grid[0].length){
						endingX = grid[0].length;
					}
					
					for (int y = startingY; y < endingY; y++){
						for (int x = startingX; x < endingX; x++){
							
							int tileId = grid[y][x];
							
							// Don't render the tile if the id is 0. This is a
							// special id.
							if (tileId != 0){
								
								Point tileChip = tiles.get(tileId);
								
								int currentX = xOffset + x * tileWidth;
								int currentY = yOffset + y * tileHeight;
								
								Rect source = new Rect(tileChip.x, 
													   tileChip.y, 
													   tileChip.x + tileWidth, 
													   tileChip.y + tileHeight);
								Rect destination = new Rect(currentX,
															currentY,
															currentX + tileWidth,
															currentY + tileHeight);
								
								c.drawBitmap(tileset, source, destination, null);
							}
						}
					}
				}
			}
		}
	}
}
